// pages/msg/msg.js
const app = getApp()
Page({
  /**
   * 页面的初始数据
   */
  data: {
    flag1:app.globalData.free_time_flag, // 判断用户今天是否已经选择了空闲的时间d
    free_time:app.globalData.free_time,
    free_time_urn:app.globalData.free_time_urn
  },
  selectFreeTime() {
    wx.showActionSheet({
      itemList: ['17:00', '18:00', '19:00', '20:00', '21:00', '22:00'],
      success: (res1) => {
        wx.showModal({
          title: '提示',
          content: '是否选择'+['17:00', '18:00', '19:00', '20:00', '21:00', '22:00'][res1.tapIndex],
          complete: (res) => {
            if (res.cancel) {
            }
            // 选择确定后执行
            if (res.confirm) {
              // 如何今天还没有选择
              if(app.globalData.free_time_flag == "0"){
                wx.request({
                  method:'POST',
                  url: app.globalData.serverPort+'/user/updataProperty',
                  data:{
                    property_name: "free_time_flag",
                    property_value: "1",
                    phone_num: app.globalData.phone_num
                  },
                  success:res=>{
                    app.globalData.free_time_flag = "1"
                    this.setData({
                      flag1: app.globalData.free_time_flag
                    })
                    console.log("修改完后的free_time_flag:"+app.globalData.free_time_flag)
                  }
                })
                let time = ['17:00', '18:00', '19:00', '20:00', '21:00', '22:00'][res1.tapIndex]
                console.log(time)
                wx.request({
                  method:'POST',
                  url: app.globalData.serverPort+'/user/updataProperty',
                  data:{
                    property_name: "free_time",
                    property_value: time,
                    phone_num: app.globalData.phone_num
                  },
                  success:(res) => {
                    console.log(res.data)
                    console.log("我发送了free_time的修改请求")
                    app.globalData.free_time = time
                    this.setData({
                      free_time: time
                    })
                    let urll = null
                    let basePreUrl = 'https://smn.cn-north-4.myhuaweicloud.com/v2/a823246b24884e77ad5b11c462d32122/notifications/topics/urn%3Asmn%3Acn-north-4%3Aa823246b24884e77ad5b11c462d32122%3ATime'
                    let baseBacUrl = '/subscriptions'
                    switch(time){
                      case "17:00":console.log("17点的主题地址");urll = basePreUrl+'17'+baseBacUrl;break;
                      case "18:00":console.log("18点的主题地址");urll = basePreUrl+'18'+baseBacUrl;break;
                      case "19:00":console.log("19点的主题地址");urll = basePreUrl+'19'+baseBacUrl;break;
                      case "20:00":console.log("20点的主题地址");urll = basePreUrl+'20'+baseBacUrl;break;
                      case "21:00":console.log("21点的主题地址");urll = basePreUrl+'21'+baseBacUrl;break;
                      case "22:00":console.log("22点的主题地址");urll = basePreUrl+'22'+baseBacUrl;break;
                    }
                    // 发送华为云订阅请求
                    wx.request({
                      method:'POST',
                      url: urll,
                      data: {
                        "protocol": "sms",
                        "endpoint": app.globalData.phone_num
                      },
                      header:{
                        'X-Auth-Token':app.globalData.token1
                      },
                      success:res=>{
                        console.log("发送订阅成功")
                        console.log(res)
                        console.log(res.data.subscription_urn)
                        console.log("11111111111111")
                        // 更新urn
                        wx.request({
                          method:'POST',
                          url: app.globalData.serverPort+'/user/updataProperty',
                          data:{
                            "propertyName": "free_time_urn",
                            "propertyValue": res.data.subscription_urn,
                            "phone_num": app.globalData.phone_num
                          },
                          success:res=>{
                            console.log("成功存储urn到数据库")
                            console.log(res)
                          }
                        })
                      }
                    })
                },
                fail:res=>{
                  console.log(res.data)
                }

              })
              }
              else{
                wx.showToast({
                  title: '您今天已选择过',
                  icon:'error',
                  duration:2000
                })
              }
            
            }
          }
        })
      },
      fail: function (res) {
        console.log(res.errMsg)
      }
    })
  },
  updataFreeTime(){
    
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    wx.request({
      method:"GET",
      url: app.globalData.serverPort+'/user/getByPhone/'+app.globalData.phone_num,
      success:res=>{
        this.setData({
          flag1:res.data.data.free_time_flag,
          free_time:res.data.data.free_time,
          free_time_urn:res.data.data.free_time_urn
        })
        console.log(res.data.data.free_time_flag+" aaa "+res.data.data.free_time+" bbb "+res.data.data.free_time_urn)
      }
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})